{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test OpenAI SDK with Custom Base URL\n",
    "\n",
    "This notebook demonstrates how to use OpenAI SDK with a custom base URL starting with https://"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "import httpx\n",
    "\n",
    "client = httpx.Client( \n",
    "    verify=False\n",
    ")\n",
    "\n",
    "## 替换下 api_key 和 base_url\n",
    "client = OpenAI(\n",
    "    api_key=\"xxxx\",\n",
    "    base_url=\"https://your-custom-endpoint.com/v1\",    \n",
    ")\n",
    "\n",
    "## 替换下 model 参数\n",
    "try:\n",
    "    response = client.chat.completions.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=[\n",
    "            {\"role\": \"user\", \"content\": \"Hello, how are you?\"}\n",
    "        ]\n",
    "    )\n",
    "    print(\"Response:\", response.choices[0].message.content)\n",
    "except Exception as e:\n",
    "    print(\"Error:\", str(e))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 发起请求时跳过 HTTPS 验证\n",
    "url = \"https://example.com\"\n",
    "response = requests.get(url, verify=False)\n",
    "\n",
    "# 输出响应内容\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test streaming response\n",
    "try:\n",
    "    stream = client.chat.completions.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=[\n",
    "            {\"role\": \"user\", \"content\": \"Count from 1 to 5\"}\n",
    "        ],\n",
    "        stream=True\n",
    "    )\n",
    "    \n",
    "    for chunk in stream:\n",
    "        if chunk.choices[0].delta.content is not None:\n",
    "            print(chunk.choices[0].delta.content, end=\"\")\n",
    "except Exception as e:\n",
    "    print(\"\\nError:\", str(e))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
